<?php

namespace Laoha\Modules\Console;

use Illuminate\Console\GeneratorCommand as LaravelGeneratorCommand;
use Illuminate\Support\Str;
use Module;

abstract class GeneratorCommand extends LaravelGeneratorCommand
{
    /**
     * Parse the name and format according to the root namespace.
     *
     * @param string $name
     *
     * @return string
     */
    protected function qualifyClass($name)
    {
        try {
            $location = $this->option('location') ?: config('modules.default_location');
        }
        catch (\Exception $e) {
            $location = config('modules.default_location');
        }

        $rootNamespace = config("modules.locations.$location.namespace");

        if (Str::startsWith($name, $rootNamespace)) {
            return $name;
        }

        $name = str_replace('/', '\\', $name);

        return $this->qualifyClass(
            $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name
        );
    }

    /**
     * Get the destination class path.
     *
     * @param string $name
     *
     * @return string
     */
    protected function getPath($name)
    {
        try {
            $location = $this->option('location') ?: config('modules.default_location');
        }
        catch (\Exception $e) {
            $location = config('modules.default_location');
        }

        $slug = $this->argument('slug');
        $module = Module::location($location)->where('slug', $slug);

        // take everything after the module name in the given path (ignoring case)
        $key = array_search(strtolower($module['basename']), explode('\\', strtolower($name)));

        if ($key === false) {
            $newPath = str_replace('\\', '/', $name);
        } else {
            $newPath = implode('/', array_slice(explode('\\', $name), $key + 1));
        }

        return module_path($slug, "$newPath.php", $location);
    }

    /**
     * Replace the class name for the given stub.
     *
     * @param  string  $stub
     * @param  string  $name
     * @return string
     */
    protected function replaceClass($stub, $name)
    {
        $stub = $this->replaceCustomizeSetting($stub, $name); 
        return parent::replaceClass($stub, $name);
    }

    /**
     * 
     * @param $stub
     * @return mixed
     */
    protected function replaceCustomizeSetting($stub, $name){
		//$name = $this->argument('name');
        $slug = $this->argument('slug');
        $basename = Str::studly($slug);
		$location = $this->option('location') ?: config('modules.default_location');
        $modulenamespace   = config("modules.locations.$location.namespace");
		//$table = Str::plural(Str::snake(class_basename($this->argument('name'))));
		$mapping  = config("modules.locations.$location.mapping");

        if(!empty($this->type) && Str::endsWith($name,$this->type)){
            $real_name = Str::replaceLast($this->type,'',class_basename($name));
        }else{
            $real_name = class_basename($name);
        }
        $table_name = Str::plural(Str::snake($real_name));
        $find = [
            'DummyBasename',
            'DummyModuleNamespace',
            'DummyName',
            'DummySlug',
            'DummyLocation',
            'DummyRealName',
            'DummyTableName',
        ];
        
        $replace = [
            $basename,
            $modulenamespace,
            $name,
            $slug,
            $location,
            $real_name,
            $table_name
        ];
        foreach ($mapping as $key => $value) {
            $find[] = $key;
            $replace[]=$value;
        }
        return str_replace($find, $replace, $stub);
    }
}
